package com.winexpress.session.account;

import com.winexpress.entity.Customer;
import com.winexpress.entity.User;
import com.winexpress.helper.AESEncryptor;
import com.winexpress.helper.Config;
import com.winexpress.helper.Encryptor;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class UserSessionBean {

    @PersistenceContext
    private EntityManager em;

    public User getUserByEmail(String email) throws Exception {
        String strQuery = "SELECT d FROM User d WHERE d.email = :inEmail";
        Query query = em.createQuery(strQuery);
        query.setParameter("inEmail", AESEncryptor.encrypt(Config.seed, email));
        List<User> list = (List<User>) query.getResultList();
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public User getUserById(Long id) {
        return em.find(User.class, id);
    }

    public boolean updatePassword(User oldUser) throws Exception {
        User user = em.find(User.class, oldUser.getId());
        if (user.getPassword() != null && user.getPassword().equals(Encryptor.md5(oldUser.getPassword(), Config.seed.getBytes()))) {
            user.setPassword(oldUser.getEmail());
            em.merge(user);
            em.flush();
            return true;
        } else if (user.getPassword() == null && oldUser.getPassword().equals(Encryptor.md5("", Config.seed.getBytes()))) {
            user.setPassword(oldUser.getEmail());
            em.merge(user);
            em.flush();
            return true;
        }
        return false;
    }

    public boolean cancelAccount(User temp) throws Exception {
        User user = em.find(User.class, temp.getId());
        if (user == null) {
            return false;
        }
        if (user.getPassword().equals(temp.getPassword())) {
            user.setActivated(false);
            Random randomGenerator = new Random();
            user.setActivationCode(String.valueOf(randomGenerator.nextLong()));
            return true;
        }
        return false;
    }

    public boolean activateAccount(User temp) throws Exception {
        User user = em.find(User.class, temp.getId());
        if (user == null) {
            return false;
        }

        if (user.getActivationCode() != null && user.getActivationCode().equals(temp.getActivationCode())) {

            user.setActivated(true);
            em.persist(user);
            return true;
        }
        return false;
    }

    public String regeneratePassword(User user) throws Exception {

        String pwd = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 7);
        System.out.println(pwd);
        System.out.println(user.getFirstName());
        user.setPassword(pwd);
        em.merge(user);

        return pwd;
    }
}
